import Fluent
import FluentPostgresDriver
import Vapor
import JWT

// configures your application
public func configure(_ app: Application) throws {
    // uncomment to serve files from /Public folder
    app.middleware.use(MyErrorMiddleware())
     app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

    configDatabase(app)
    
    app.jwt.signers.use(.hs256(key: "laowu"))

    // register routes
    try routes(app)
}


func configDatabase(_ app: Application) {
    
    app.databases.use(.postgres(
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? PostgresConfiguration.ianaPortNumber,
        username: Environment.get("DATABASE_USERNAME") ?? "laowudxf",
        password: Environment.get("DATABASE_PASSWORD") ?? "laowudxf",
        database: Environment.get("DATABASE_NAME") ?? "swift_pan"
    ), as: .psql)

//    app.migrations.add(CreateTodo())
    app.migrations.add(CreateSwiftPanTable())
    
    let user = User(userName: "laowu", password: "123456")
    try? User(userName: "laowu", password: "123456").create(on: app.db).wait()
    app.logger.info( .init(stringLiteral: user.id?.uuidString ?? "") )
}
